package com.erdos.message.entity;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;

import com.erdos.common.entity.AbstractEntity;
import com.erdos.event.message.MessageEventType;

public class MessageHistory extends AbstractEntity {
	private static final long serialVersionUID = 5958775313032635363L;

	private String sender;
	private String receiver;
	private String messageTypeId;
	private String messageTypeName;
	private String correlationMessageTypeId;
	private String correlationMessageId;
	private String replyToMessageId;
	private String destination;
	private String messageId;
	private String type;
	private Date sendDt;
	private Date receiveDt;
	private String state;
	private String message;
	private String exception;
	private String jmsMessageId;
	private String replyMsgInfo;

	/**
	 * @return the sender
	 */
	public String getSender() {
		return sender;
	}

	/**
	 * @param sender the sender to set
	 */
	public void setSender(String sender) {
		this.sender = sender;
	}

	/**
	 * @return the receiver
	 */
	public String getReceiver() {
		return receiver;
	}

	/**
	 * @param receiver the receiver to set
	 */
	public void setReceiver(String receiver) {
		this.receiver = receiver;
	}

	/**
	 * @return the messageTypeId
	 */
	public String getMessageTypeId() {
		return messageTypeId;
	}

	/**
	 * @param messageTypeId the messageTypeId to set
	 */
	public void setMessageTypeId(String messageTypeId) {
		this.messageTypeId = messageTypeId;
	}

	/**
	 * @return the destination
	 */
	public String getDestination() {
		return destination;
	}

	/**
	 * @param destination the destination to set
	 */
	public void setDestination(String destination) {
		this.destination = destination;
	}

	/**
	 * @return the type
	 */
	public String getType() {
		return type;
	}

	/**
	 * @param type the type to set
	 */
	public void setType(String type) {
		this.type = type;
	}

	/**
	 * @return the sendDt
	 */
	public Date getSendDt() {
		return sendDt;
	}

	/**
	 * @param sendDt the sendDt to set
	 */
	public void setSendDt(Date sendDt) {
		this.sendDt = sendDt;
	}

	/**
	 * @return the receiveDt
	 */
	public Date getReceiveDt() {
		return receiveDt;
	}

	/**
	 * @param receiveDt the receiveDt to set
	 */
	public void setReceiveDt(Date receiveDt) {
		this.receiveDt = receiveDt;
	}

	/**
	 * @return the state
	 */
	public String getState() {
		return state;
	}

	/**
	 * @param state the state to set
	 */
	public void setState(String state) {
		this.state = state;
	}

	/**
	 * @return the message
	 */
	public String getMessage() {
		return message;
	}

	/**
	 * @param message the message to set
	 */
	public void setMessage(String message) {
		this.message = message;
	}

	/**
	 * @return the exception
	 */
	public String getException() {
		return exception;
	}

	/**
	 * @param exception the exception to set
	 */
	public void setException(String exception) {
		this.exception = exception;
	}

	/**
	 * @return the messageId
	 */
	public String getMessageId() {
		return messageId;
	}

	/**
	 * @param messageId the messageId to set
	 */
	public void setMessageId(String messageId) {
		this.messageId = messageId;
	}

	/**
	 * @return the messageTypeName
	 */
	public String getMessageTypeName() {
		return messageTypeName;
	}

	/**
	 * @param messageTypeName the messageTypeName to set
	 */
	public void setMessageTypeName(String messageTypeName) {
		this.messageTypeName = messageTypeName;
	}

	/**
	 * @return the correlationMessageTypeId
	 */
	public String getCorrelationMessageTypeId() {
		return correlationMessageTypeId;
	}

	/**
	 * @param correlationMessageTypeId the correlationMessageTypeId to set
	 */
	public void setCorrelationMessageTypeId(String correlationMessageTypeId) {
		this.correlationMessageTypeId = correlationMessageTypeId;
	}

	/**
	 * @return the correlationMessageId
	 */
	public String getCorrelationMessageId() {
		return correlationMessageId;
	}

	/**
	 * @param correlationMessageId the correlationMessageId to set
	 */
	public void setCorrelationMessageId(String correlationMessageId) {
		this.correlationMessageId = correlationMessageId;
	}

	/**
	 * @return the replyToMessageId
	 */
	public String getReplyToMessageId() {
		return replyToMessageId;
	}

	/**
	 * @param replyToMessageId the replyToMessageId to set
	 */
	public void setReplyToMessageId(String replyToMessageId) {
		this.replyToMessageId = replyToMessageId;
	}

	public String getJmsMessageId() {
		return jmsMessageId;
	}

	public void setJmsMessageId(String jmsMessageId) {
		this.jmsMessageId = jmsMessageId;
	}
   
	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((messageId == null) ? 0 : messageId.hashCode());
		return result;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof MessageHistory))
			return false;
		final MessageHistory other = (MessageHistory) obj;
		if (messageId == null) {
			if (other.messageId != null)
				return false;
		} else if (!messageId.equals(other.messageId))
			return false;
		return true;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return new ToStringBuilder(this)
				.append("messageId", getMessageId())
				.append("messageTypeId", getMessageTypeId())
				.append("messageTypeName", getMessageTypeName())
				.append("message", getMessage())
				.append("correlationMessageId", getCorrelationMessageId())
				.append("correlationMessageTypeId",
						getCorrelationMessageTypeId())
				.append("replyToMessageId", getReplyToMessageId())
				.append("receiver", getReceiver())
				.append("receiveDt", getReceiveDt())
				.append("sender", getSender())
				.append(
						"sendDt",(this.getSendDt() == null ? "" : new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(this.getSendDt())))
				.append("destination", getDestination()).append("jmsMessageId",
						getJmsMessageId()).append("type", getType()).append(
						"exception", getException()).append("createdBy",
						getCreatedBy()).append(
						"createdDt",(getCreatedDt() == null ? "" : new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(this.getCreatedDt()))).append("updatedBy",
						getUpdatedBy()).append(
						"updatedDt",(getUpdatedDt() == null ? "" : new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(this.getUpdatedDt()))).toString();

	}
	
	public String getSendDtStr(){
		if(this.getSendDt()==null){
			return "";
		}
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(this.getSendDt());
	}
	
	public String getReceiveDtStr(){
		if(this.getReceiveDt()==null){
			return "";
		}
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(this.getReceiveDt());
	}
	
	public boolean isLinkToEventLogAble(){
		boolean linkAble = MessageEventType.valueOf(this.state).compareTo(MessageEventType.PRE_PROCESSING) > 0;
		if( MessageEventType.valueOf(this.state).equals(MessageEventType.EXCEPTION)){
		   String currState = StringUtils.substringBefore(exception, ":");
		   if(MessageEventType.valueOf(currState).equals(MessageEventType.PROCESSING)){
			   linkAble = true;
		   }
		}
		return linkAble;
	}

	/**
	 * @return the replyMsgInfo
	 */
	public String getReplyMsgInfo() {
		return replyMsgInfo;
	}

	/**
	 * @param replyMsgInfo the replyMsgInfo to set
	 */
	public void setReplyMsgInfo(String replyMsgInfo) {
		this.replyMsgInfo = replyMsgInfo;
	}
	
	
}
